<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" class="site-navbar__menu--right">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="项目名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('generator:project:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!--<el-button v-if="isAuth('generator:project:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量撤销</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="projectName"
        header-align="center"
        align="center"
        label="项目名称">
      </el-table-column>
      <el-table-column
        prop="projectDesc"
        header-align="center"
        align="center"
        label="项目简介">
      </el-table-column>
      <el-table-column
        prop="projectHighlight"
        header-align="center"
        align="center"
        label="项目亮点">
      </el-table-column>
      <el-table-column
        prop="projectTime"
        header-align="center"
        align="center"
        label="项目时间">
      </el-table-column>
      <el-table-column
        prop="companyName"
        header-align="center"
        align="center"
        label="所属公司">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="申报人">
      </el-table-column>
      <el-table-column
        prop="prizeName"
        header-align="center"
        align="center"
        label="申请奖项">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        label="申报状态">
        <template scope="scope">
            <p v-if="scope.row.state=='0'">草稿</p>
            <p v-if="scope.row.state=='1'">审核中</p>
            <p v-if="scope.row.state=='2'">通过</p>
            <p v-if="scope.row.state=='3'">不通过</p>
            <p v-if="scope.row.state=='4'">已分配专家</p>
            <p v-if="scope.row.state=='5'">专家评分完成</p>
        </template>
      </el-table-column>
      <!--(0:草稿，1:审核中，2:通过，3:不通过)-->
      <el-table-column
        prop="mark"
        header-align="center"
        align="center"
        label="最终评分">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-if="isAuth('generator:project:look')" size="small" @click="lookHandle(scope.row.id)">查看</el-button>
          <el-button type="text" v-if="isAuth('generator:project:approval') && scope.row.state == 0 && sessionUserId != 1" size="small" @click="approvalHandle(scope.row.id)">提交审核</el-button>
          <el-button type="text" v-if="isAuth('generator:project:update') && (scope.row.state == 0 || scope.row.state == 3)  && sessionUserId != 1" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" v-if="isAuth('generator:projectfile:save') && (scope.row.state == 0 || scope.row.state == 3)  && sessionUserId != 1" size="small" @click="uploadFileHandle(scope.row.id)">上传附件</el-button>
          <el-button type="text" v-if="isAuth('generator:project:assignExperts') && scope.row.state == 2" size="small" @click="assignExpertsHandle(scope.row.id)">分配专家</el-button>

          <el-button type="text" v-if="isAuth('generator:project:toPass') && scope.row.state == 1" size="small" @click="toPassHandle(scope.row.id)">通过</el-button>
          <el-button type="text" v-if="isAuth('generator:project:toNoPass') && scope.row.state == 1" size="small" @click="toNoPassHandle(scope.row.id)">不通过</el-button>
          <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.id)">撤销</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 上传附件 -->
    <uploadFile v-if="uploadFileVisible" ref="uploadFile" @refreshDataList="getDataList"></uploadFile>
    <!-- 查看 -->
    <look v-if="lookVisible" ref="look" @refreshDataList="getDataList"></look>
    <!-- 分配专家 -->
    <assign-experts v-if="assignExpertsVisible" ref="assignExperts" @refreshDataList="getDataList"></assign-experts>
  </div>
</template>

<script>
  import AddOrUpdate from './project-add-or-update'
  import Look from './project-look'
  import UploadFile from './projectfile-add-or-update'
  import AssignExperts from './project-assign-experts'
  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },
        sessionUserId: '',
        prizes: [],
        experts: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        uploadFileVisible: false,
        lookVisible: false,
        assignExpertsVisible: false
      }
    },
    components: {
      AddOrUpdate,
      UploadFile,
      Look,
      AssignExperts
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.sessionUserId = this.$store.state.user.id
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/generator/project/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
            console.log(data.experts)
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            this.prizes = data.prizes
            this.experts = data.experts
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, this.prizes, this.experts)
        })
      },
      // 跳转上传附件
      uploadFileHandle (id) {
        this.uploadFileVisible = true
        this.$nextTick(() => {
          console.log('上传附件点击了....')
          console.log(this.$refs)
          console.log(this.$refs.uploadFile)
          this.$refs.uploadFile.init(id)
        })
      },
      // 查看
      lookHandle (id) {
        this.lookVisible = true
        this.$nextTick(() => {
          this.$http({
            url: this.$http.adornUrl(`/generator/project/searchExperts/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$nextTick(() => {
                this.$refs.look.init(id, data.prizes)
              })
            } else {
              this.$message.error(data.msg)
            }
          })
          
        })
      },
      // 分配专家
      assignExpertsHandle (id) {
        this.assignExpertsVisible = true
        this.$http({
            url: this.$http.adornUrl(`/generator/project/searchExperts/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$nextTick(() => {
                this.$refs.assignExperts.init(id, data.userList, data.prizes)
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      },
      // 提交审核
      approvalHandle (id) {
        this.$http({
            url: this.$http.adornUrl(`/generator/project/approval/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      },
      // 通过
      toPassHandle (id) {
        this.$http({
            url: this.$http.adornUrl(`/generator/project/toPass/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      },
      // 不通过
      toNoPassHandle (id) {
        this.$http({
            url: this.$http.adornUrl(`/generator/project/toNoPass/${id}`),
            method: 'get'
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
      },
      // 撤销
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '撤销' : '批量撤销'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/generator/project/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
